package com.example.ch02_student;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ch02_student.entity.Admins;
import com.example.ch02_student.entity.Student;
import com.example.ch02_student.entity.Teacher;
import com.example.ch02_student.utils.DataBean;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.ResultVo1;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;
import com.google.gson.JsonObject;


import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;


import java.io.IOException;
import java.util.List;

import lombok.SneakyThrows;
import lombok.ToString;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Login extends AppCompatActivity {
    OkHttpClient okHttpClient;
    EditText username,password;
   static String  urlBase="http://10.23.4.210:8080";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        okHttpClient=new OkHttpClient();
        username=findViewById(R.id.et_user);
        password=findViewById(R.id.editText1);

    }

    //登录按钮
    public void login1(View view){
        final String un = username.getText().toString();
        final String ps = password.getText().toString();
        UrlBase urlBase = new UrlBase();

        final Request build = new Request.Builder().url(UrlBase.URL_BASE+"/users/login?no="+un+"&password="+ps).build();
        System.err.println(UrlBase.URL_BASE+"/users/login?no="+username.getText().toString()+"&password="+password.getText().toString());
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @SneakyThrows
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                System.err.println(string);
                Gson gson = new Gson();
                System.err.println(string);
                    JSONObject jsonObject = new JSONObject(string);
                    String data = jsonObject.getString("data");
                    if (jsonObject.getInt("code") == 1000) {
                        final Admins admins;
                        Teacher teacher;
                        if (jsonObject.getString("message").equals("管理员")) {
                            admins = gson.fromJson(data, Admins.class);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent(Login.this, AdminsMain.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("ano", un);
                                    bundle.putString("password", ps);
                                    bundle.putString("aname",admins.getAname());
                                    bundle.putString("sex",admins.getSex());
                                    bundle.putInt("age",admins.getAge());
                                    bundle.putString("address",admins.getAddress());
                                    bundle.putString("tel",admins.getTel());

                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            });
                        }
                        else if (jsonObject.getString("message").equals("教师")) {
                            teacher = gson.fromJson(data, Teacher.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("teacher",teacher);
                            startActivity(new Intent(Login.this,TeacherMain.class).putExtras(bundle));
                        }
                        else if (jsonObject.getString("message").equals("学生")) {
                            Student student = gson.fromJson(data, Student.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("student",student);
                            startActivity(new Intent(Login.this,StudentMain.class).putExtras(bundle));
                        }


                    } else {
                        //账号或密码错误
                        Looper.prepare();
                        Toast.makeText(Login.this, "error", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }
                }




        });

    }


}
